package com.wontube.smr.conf.dogs;

import java.io.File;

import org.slf4j.LoggerFactory;

import com.wontube.core.conf.dogs.FileWatchDog;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

/**
 * @author yuechao
 * 
 */
public class LogbackWatchDog extends FileWatchDog {
	/**
	 * @param filename
	 */
	public LogbackWatchDog(String confPath) {
		super(confPath);
	}

	/*
	 * @see core.pub.FileWatchDog#doOnChange()
	 */
	@Override
	protected void doOnChange() {
		this.loadConfig(super.file);
	}
	
	private boolean loadConfig(File configFile) {
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		JoranConfigurator configurator = new JoranConfigurator();
		configurator.setContext(lc);
		lc.reset();
		try {
			configurator.doConfigure(configFile);
		} catch (JoranException e) {
			return false;
		}
		
		return true;
	}

	@Override
	protected String getFilename() {
		return "logback.xml";
	}
}
